//
//  ZDQMainSendViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZDQMainSendViewController.h"
#import <MessageUI/MessageUI.h>
#import "MobClick.h"

@interface ZDQMainSendViewController ()<MFMailComposeViewControllerDelegate, UITextViewDelegate>

@property(nonatomic, strong) UITextView *textView;
@property(nonatomic, strong) UIButton *sendButton;

@end

@implementation ZDQMainSendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationController.navigationBar.translucent = NO;
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict = [NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.title = @"意见反馈";
    
    // Do any additional setup after loading the view.
    
    self.textView = [[UITextView alloc] init];
    [self.view addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(10);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(self.view.mas_height).with.offset(-KHEIGHT / 6);
    }];
    self.textView.font = [UIFont systemFontOfSize:25];
    self.textView.delegate = self;
//    self.textView.backgroundColor = [UIColor cyanColor];
//        self.textView.layer.borderWidth = 1;
    
    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.sendButton];
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.textView.mas_bottom).with.offset(35 * KHEIGHT / 667);
        make.left.equalTo(self.view).with.offset(100);
        make.right.equalTo(self.view).with.offset(-100);
        make.bottom.equalTo(self.view).with.offset(-25 * KHEIGHT / 667);
        
    }];
    
    [self.sendButton setTitle:@"发表意见" forState:UIControlStateNormal];
    [self.sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.sendButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.sendButton.layer.borderWidth = 2;
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:20 * KHEIGHT / 667];
    self.sendButton.layer.borderColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1].CGColor;
    
    UILabel *descripeLabel = [[UILabel alloc] init];
    [self.view addSubview:descripeLabel];
    descripeLabel.tag = 1000;
    [descripeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).with.offset(230 * KHEIGHT / 667);
        make.bottom.equalTo(self.view).with.offset(-320 * KHEIGHT / 667);
        make.left.equalTo(self.view).with.offset(50 * KWIDTH / 375);
        make.right.equalTo(self.view).with.offset(-50 * KWIDTH / 375);
        
    }];
    
    descripeLabel.text = @"欢迎反馈, 上传截图便于定位问题";
    descripeLabel.textAlignment = NSTextAlignmentCenter;
    descripeLabel.font = [UIFont systemFontOfSize:15];
    
    if ([ZDQSingleton shareSingleton].isLongin == YES) {
        self.textView.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
        self.textView.textColor = [UIColor whiteColor];
        self.view.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
        [self.sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        self.textView.backgroundColor = [UIColor whiteColor];
        self.textView.textColor = [UIColor blackColor];
        self.textView.backgroundColor = [UIColor whiteColor];
        [self.sendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    
    
}



#pragma mark 当textView开始编辑的时候
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    UILabel *label = (UILabel *)[self.view viewWithTag:1000];
    label.hidden = YES;
    
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).with.offset(300 * KHEIGHT / 667);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(self.view.mas_height).with.offset(-KHEIGHT / 6- 256 * KHEIGHT / 667);
    }];
    self.textView.layer.borderWidth = 1;

    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.textView.mas_bottom).with.offset(5 * KHEIGHT / 667);
        make.left.equalTo(self.view).with.offset(100);
        make.right.equalTo(self.view).with.offset(-100);
        make.bottom.equalTo(self.view).with.offset(-5 * KHEIGHT / 667);
        
    }];
    
    self.sendButton.layer.borderWidth = 2;
    self.sendButton.layer.borderColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1].CGColor;
    self.navigationController.navigationBar.hidden = YES;
    if ([self.textView.text isEqualToString:@""]) {
    } else {
        
    [self.sendButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}

#pragma mark button上移的方法
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
   
    // 如果textField的位置Y点(高低点)低于屏幕的一半
    if (self.sendButton.frame.origin.y > self.view.frame.size.height / 2) {
        // 改变整个视图的中心点位置 屏幕上移
        [UIView animateWithDuration:0.2 animations:^{
            self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 252);
        }];
    }
    
    
    return YES;
}




#pragma mark button的点击方法
- (void)buttonAction:(UIButton *)button {
    if (self.textView.text.length != 0) {
        Class mailClass = NSClassFromString(@"MFMailComposeViewController");
        if (mailClass) {
            if ([mailClass canSendMail]) {
                [self displayMailBox];
            }
            else {
                [self showMailBoxOnDevice];
            }
        }
        else {
            [self showMailBoxOnDevice];
        }
    }
    else {
        if (self.view.frame.origin.y == -252) {
            [self.textView resignFirstResponder];
            [UIView animateWithDuration:0.2 animations:^{
                self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 252);
            }];
        }
        
    }

    
}

- (void)displayMailBox {
    // 创建邮件视图
    MFMailComposeViewController *mailBox = [[MFMailComposeViewController alloc] init];
    // 添加收件地址
    NSArray *recipient = [NSArray arrayWithObject:@"375212214@qq.com"];
    [mailBox setToRecipients:recipient];
    // 填写主题
    [mailBox setSubject:@"意见反馈"];
    // 邮件内容
    NSString *messageBody = self.textView.text;
    [mailBox setMessageBody:messageBody isHTML:YES];
    // 设置代理人
    mailBox.mailComposeDelegate = self;
    [self presentViewController:mailBox animated:YES completion:^{
        
    }];
}

- (void)showMailBoxOnDevice {
    NSString *mail = @"mailto:375212214@qq.com";
    mail = [mail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:mail]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *message;
    
    switch (result) {
        case MFMailComposeResultCancelled:
        {
            message = @"取消发送";
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"取消发送" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *Action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertController addAction:Action];
            [self presentViewController:alertController animated:YES completion:^{
            }];

            NSLog(@"%@", message);
        }
            
            break;
        case MFMailComposeResultSaved:
        {
            message = @"保存草稿";
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"保存草稿" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *Action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertController addAction:Action];
            [self presentViewController:alertController animated:YES completion:^{
            }];

            NSLog(@"%@", message);
        }
            break;
        case MFMailComposeResultSent:
        {
            message = @"已发送";
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"已发送" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *Action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertController addAction:Action];
            [self presentViewController:alertController animated:YES completion:^{
            }];

            NSLog(@"%@", message);
        }
            break;
        case MFMailComposeResultFailed:
        {
            message = @"发送失败";
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"发送失败" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *Action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertController addAction:Action];
            [self presentViewController:alertController animated:YES completion:^{
            }];

            NSLog(@"%@ 错误信息:%@", message, error);
        }
            break;
    }
    
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}


- (void)viewWillAppear:(BOOL)animated {
    
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(10);
        make.left.equalTo(self.view).with.offset(10);
        make.right.equalTo(self.view).with.offset(-10);
        make.height.equalTo(self.view.mas_height).with.offset(-KHEIGHT / 6);
    }];
    
    self.textView.text = @"";
    self.textView.layer.borderWidth = 0;
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.textView.mas_bottom).with.offset(35 * KHEIGHT / 667);
        make.left.equalTo(self.view).with.offset(100);
        make.right.equalTo(self.view).with.offset(-100);
        make.bottom.equalTo(self.view).with.offset(-25 * KHEIGHT / 667);
        
    }];

    UILabel *descrpeLabel = (UILabel *)[self.view viewWithTag:1000];
    descrpeLabel.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
